import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# 1. 创建Series
s1 = pd.Series([10,20,30,40], index=["a","b","c","d"])

# 2. 创建DataFrame
data = {
    "姓名": ["张三","李四","王五","赵六","钱七"],
    "年龄": [18,19,18,20,19],
    "语文成绩": [85,92,78,90,88],
    "数学成绩": [90,85,82,95,86]
}
df = pd.DataFrame(data)

# 数据准备
students = df["姓名"]
math_scores = df["数学成绩"]

# 中文字体设置（避免乱码）
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False


chinese = df["语文成绩"]
math = df["数学成绩"]
plt.figure(figsize=(10,6))
# 散点图
plt.scatter(chinese, math, s=100, alpha=0.7, color='#d62728', label='学生成绩')
# 趋势线
z = np.polyfit(chinese, math, 1)
p = np.poly1d(z)
plt.plot(chinese, p(chinese), 'r--', label=f'趋势线: y={z[0]:.2f}x+{z[1]:.2f}')
plt.title('语文与数学成绩相关性', fontsize=14)
plt.xlabel('语文成绩（分）')
plt.ylabel('数学成绩（分）')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()